(function() {
    "use strict";
    var getByClass = function(className) {
            /*通过class获取元素*/
            if (document.getElementsByClassName) {
                return document.getElementsByClassName(className);
            } else {
                var result = [],
                    obj = document.getElementsByTagName("*"),
                    reg = new RegExp('\\b' + className + '\\b', 'i');
                for (var index = 0; index < obj.length; index++) {
                    if (reg.test(obj[index].className)) {
                        result.push(obj[index]);
                    }
                }
            }
            return result;
        },
        inputFilter = function(ele) {
            /*判断值的范围与类型*/
            if (ele.value) {
                if (parseInt(ele.value) != ele.value) {
                    /*判断是否数字*/
                    alert("格式错误，请输入一个介于 0 和 255 间的数字。");
                    ele.value = "";
                } else {
                    if (ele.value > 255) {
                        ele.value = 255;
                        alert(ele.value + "不是有效值，请指定一个介于 0 和 255 间的值。");
                    }
                }
            }
        };

    if ("\v" === "v") { 
        /*IE8及以下的兼容*/
        var ipInputList = getByClass("ip");
        for (var index = ipInputList.length; index--;) {
            var ele = ipInputList[index];
            /*绑定事件*/
            ele.onpropertychange = function() {
                return inputFilter(this);
            };
        }
    }

    $(document).on("keydown", ".ip", function(event) {
        /*禁止非法字符的输入*/
        var k = event.which;
        if ((k >= 48 && k <= 57) || (k >= 96 && k <= 105) || k === 8 || k === 9 || k === 37 || k === 39 || k === 17 || k === 86) {
            if (this.value.length > 2) {
                /*限制输入长度*/
                return k === 8 || k === 9 || k === 37 || k === 39;
            }
        } else {
            return false;
        }
    }).on("input", ".ip", function() {
        /*通用事件*/
        return inputFilter(this);
    }).on("focus", ".ip", function() {
        /*外层边框:active*/
        $(this).parent("div.form-control").css("border-color", "#2c3150");
    }).on("blur", ".ip", function() {
        $(this).parent("div.form-control").css("border-color", "#dce4ec");
    });
}());